//
//  TableDataSourceToArray.m
//  Private_Object
//
//  Created by pro on 16/7/30.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByArrayTableDataSource.h"


@implementation ByArrayTableDataSource
{
    NSString *_identifier;
}

- (id)initWithTable:(UITableView *)table reusableCellClass:(Class)reusableCellClass {
    if (self = [super init]) {
        table.dataSource = self;
        table.delegate = self;
        _identifier = NSStringFromClass(reusableCellClass);
        [table registerClass:reusableCellClass forCellReuseIdentifier:_identifier];
    }
    return self;
}

#pragma mark - default
// getter
- (NSArray *)itemsAry {
    if (!_itemsAry) {
        _itemsAry = @[];
    }
    return _itemsAry;
}
- (CGFloat)rowHeight {
    
    if (_rowHeight == 0) {
        _rowHeight = 44;
    }
    return _rowHeight;
}

- (CGFloat)headerHeight {
    if (_headerHeight == 0) {
        _headerHeight = 8;
    }
    return _headerHeight;
}



/** 根据indexPath获取数据 */
- (id)itemForIndexPath:(NSIndexPath *)indexPath {
    NSInteger index = self.singleSection ? indexPath.row : indexPath.section;
    return self.itemsAry[index];
}

#pragma mark - datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.singleSection ? 1 : self.itemsAry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return !self.singleSection ? 1 : self.itemsAry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_identifier];
    /** 设置单元格属性 */
    if (self.cellConfig) {
        self.cellConfig(cell, [self itemForIndexPath:indexPath]);
    }
    return cell;
}

#pragma mark - delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    /** 设置单元格高度 */
    if (self.autoHeightLayout) {
        return self.autoHeightLayout([self itemForIndexPath:indexPath]);
    } else {
        return self.rowHeight;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView.style == UITableViewStylePlain) {
        return 0;
    } else {
        if (section == 0) {
            return 0.01;
        } else {
            return self.headerHeight / 2;
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (tableView.style == UITableViewStylePlain) {
        return self.headerHeight;
    } else {
        if (section == self.itemsAry.count - 1) {
            return 0.01;
        }
        return self.headerHeight / 2;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (self.cellDidSelect) {
        self.cellDidSelect(self.singleSection ? indexPath.row : indexPath.section);
    }
}
@end
